package xxl

import (
	"context"
	"github.com/xxl-job/xxl-job-executor-go"
	"github.com/zeromicro/go-zero/core/logx"
	tools "go-zero-project/common/utils"
	logicApi "go-zero-project/services/user/api/internal/logic/user"
	"go-zero-project/services/user/api/internal/svc"
	"go-zero-project/services/user/api/internal/types"
)

// RegisterTask 注册定时任务
func RegisterTask(serverCtx *svc.ServiceContext) {
	executor := serverCtx.XxlExecutor
	if executor == nil {
		logx.Error("executor init is empty")
		return
	}
	// 这里可以根据配置文件进行读取 路由规则 如果存在多个定时任务直接在这里处理
	executor.RegTask("task.test", func(cxt context.Context, param *xxl.RunReq) string {
		userInfoLogic := logicApi.NewUserInfoLogic(cxt, serverCtx)
		executorParams := param.ExecutorParams
		userReq := new(types.UserInfoReq)
		tools.JsonToBean(executorParams, userReq)
		info, err := userInfoLogic.UserInfo(userReq)
		if err != nil {
			logx.Error(err)
			return err.Error()
		}
		logx.Infof(tools.BeanToJson(info))
		return "my test done"
	})
}
